package com.zzc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzc.bean.Employee;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("LoginInterceptor");
        String url = request.getRequestURI();
        System.out.println("拦截"+url);
        String host = request.getRemoteHost();
        /*
         * 从session中获取用户信息
         */
        Employee employee = (Employee) request.getSession().getAttribute("employee");

        if(employee == null){//如果session中没有用户的信息，跳转到登录页面，内部网页不能访问
            System.out.println("未登陆用户，拦截");
            response.sendRedirect(request.getContextPath()+"/view/index.jsp");
            return false;
        }else{
            System.out.println("已登陆用户，跳过拦截");
            return true;
        }
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

        System.out.println("postHandle run!");
    }
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

        System.out.println("afterCompletion run!");
    }

}